<template>
  <div class="user-approval">
    <el-container>
      <el-header>
        <div class="header-content">
          <div class="header-left">
            <el-button type="primary" @click="router.push('/')">
              <el-icon><ArrowLeft /></el-icon>
              返回主页
            </el-button>
          </div>
          <div class="header-center">
            <img src="https://susu-plants.oss-cn-beijing.aliyuncs.com/2025-03-04-22-13-33QQ20250304-190757.png" alt="Logo" class="logo-image">
            <h2>用户状态审批</h2>
          </div>
        </div>
      </el-header>
      <el-main>
        <h2>用户申请审批</h2>
        <el-table :data="applyUsers" style="width: 100%">
          <el-table-column label="头像" width="80">
            <template #default="scope">
              <el-avatar 
                :size="40" 
                :src="scope.row.image_url || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
                @error="() => {
                  console.log('头像加载失败:', {
                    userId: scope.row.user_id,
                    imageUrl: scope.row.image_url
                  })
                }"
              />
            </template>
          </el-table-column>
          <el-table-column prop="user_name" label="用户名" />
          <el-table-column prop="call_name" label="昵称" />
          <el-table-column prop="phone_number" label="手机号" />
          <el-table-column prop="role_name" label="角色" />
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button type="success" size="small" @click="handleUserApply(scope.row.user_id, 1)">
                通过
              </el-button>
              <el-button type="danger" size="small" @click="handleUserApply(scope.row.user_id, 0)">
                拒绝
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getApplyTypeUsers, adminUpdateApplyType, logout } from '@/api/user'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const applyUsers = ref([])

// 加载待审核用户列表
const loadApplyUsers = async () => {
  try {
    console.log('开始加载待审核用户列表...')
    const res = await getApplyTypeUsers()
    console.log('获取到的用户列表数据:', res)
    applyUsers.value = res
    // 检查每个用户的头像数据
    applyUsers.value.forEach(user => {
      console.log('用户头像信息:', {
        userId: user.user_id,
        userName: user.user_name,
        imageUrl: user.image_url,
        fullUserData: user
      })
    })
  } catch (error) {
    console.error('加载待审核用户失败:', error)
    ElMessage.error('加载待审核用户失败，请重试')
  }
}

// 处理用户申请
const handleUserApply = async (userId, applyType) => {
  try {
    await adminUpdateApplyType(userId, applyType)
    ElMessage.success('处理成功')
    loadApplyUsers()
  } catch (error) {
    console.error('处理失败:', error)
    ElMessage.error('处理失败，请重试')
  }
}

// 返回首页
const handleBack = () => {
  router.push('/')
}

// 用户登出
const handleLogout = async () => {
  try {
    await logout()
    userStore.clearUserInfo()
    ElMessage.success('退出登录成功')
    router.push('/')
  } catch (error) {
    console.error('退出登录失败:', error)
    ElMessage.error('退出登录失败，请重试')
  }
}

onMounted(() => {
  loadApplyUsers()
})
</script>

<style scoped>
.user-approval {
  min-height: 100vh;
}

.el-header {
  padding: 0;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.header-left {
  flex: 0 0 auto;
}

.header-center {
  flex: 1;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.logo-image {
  height: 40px;
  width: auto;
}

.header-center h2 {
  margin: 0;
  color: #409EFF;
}

.el-main {
  padding: 20px;
  max-width: 80%;
  margin: 0 auto;
  margin: 80px auto 0;
}

h2 {
  margin-bottom: 20px;
}
</style>